package com.my.myupdata.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * 项目名称：权限申请工具
 * 创建人：MWB
 * 创建时间：2018/6/11 9:23
 */

public class MWBPermissionUtil {
    //    private Context context;
    private static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;

    /**
     * 单条权限申请
     *
     * @param context
     * @param singlePermission
     * @return boolean
     */
    public boolean checkSinglePermission(Context context, String singlePermission) {
        // 检查是否拥有该权限
        if (ContextCompat.checkSelfPermission(context, singlePermission) !=
                PackageManager.PERMISSION_GRANTED) {
            // 拥有该权限
            return false;
        } else {
//            applySinglePermisson(context, singlePermission);
            return true;
        }
    }

    /**
     * 申请单条权限
     *
     * @param context
     * @param singlePermisson
     */
    public void applySinglePermisson(Context context, String singlePermisson) {
        ActivityCompat.requestPermissions((Activity) context,
                new String[]{singlePermisson},
                MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
    }
}
